<?php

namespace common\models\freight;

use Yii;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%freight_receiver}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property string $name 收件人姓名
 * @property string $mobile 收件人手机号
 * @property string $province 省份
 * @property string $city 市
 * @property string $district 地区
 * @property string $address 详细地址
 * @property string $from 来源标识
 * @property string $mark 备注信息
 * @property string $price 运费（元）
 * @property string $discount 优惠运费（元）
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class FreightReceiver extends \common\db\ActiveRecord {

    const FROM_SHOP = 'shop';
    const FROM_CARD = 'card';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%freight_receiver}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['name', 'mobile', 'province', 'city', 'district', 'address', 'from'], 'required'],
            [['price', 'discount'], 'number'],
            [['name', 'mobile', 'province', 'city', 'district'], 'string', 'max' => 50],
            [['address'], 'string', 'max' => 255],
            [['from'], 'string', 'max' => 20],
            [['mark'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'name' => '收件人姓名',
            'mobile' => '收件人手机号',
            'province' => '省份',
            'city' => '市',
            'district' => '地区',
            'address' => '详细地址',
            'from' => '来源标识',
            'mark' => '备注信息',
            'price' => '运费（元）',
            'discount' => '优惠运费（元）',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function fields() {
        return [
            'id',
            'name',
            'telephone' => function() {
                return StringHelper::maskChar($this->mobile);
            },
            'province',
            'city',
            'district',
            'address',
            'detail' => function() {
                return $this->province . $this->city . $this->district . ' ' . $this->address;
            }
        ];
    }

}
